﻿//www.codeOften.com
//Senthil Maruthaiappan - senips (at) gmail.com
//see license on http://www.codeplex.com/Jinglebell

using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace JingleBell.Interface
{
    public enum CommandTypes
    {
        VisualStudioCommand,
        DelphiCommand,
        GeneralCommand
    } ;

    [XmlRoot("Commands")]
    public class CommandItems
    {
        private readonly IBindingList _commandList = new BindingList<CommandItem>();

        public string ConfigFile { set; get; }

        [XmlIgnore]
        public IBindingList CommandList
        {
            get { return _commandList; }
        }

        public CommandItem[] Commands
        {
            get
            {
                var items = CommandList as BindingList<CommandItem>;
                return items.ToArray();
            }
            set
            {
                CommandList.Clear();
                foreach (CommandItem item in value)
                {
                    CommandList.Add(item);
                }
            }
        }

        public void Save()
        {
            if (string.IsNullOrEmpty(ConfigFile))
                throw new Exception("Configuration File must be specified");
            SaveSettings(this, ConfigFile);
        }

        public static void SaveSettings(CommandItems settingsObj, string fileName)
        {
            var xs = new XmlSerializer(typeof (CommandItems));
            using (var xw = new XmlTextWriter(fileName, Encoding.UTF8))
            {
                xs.Serialize(xw, settingsObj);
                xw.Close();
            }
        }

        public static CommandItems LoadSettings(string fileName)
        {
            var commandItems = new CommandItems();
            if (!File.Exists(fileName))
            {
                commandItems.ConfigFile = fileName;
                commandItems.Save();
                return commandItems;
            }

            var xs = new XmlSerializer(typeof (CommandItems));
            using (var xr = new XmlTextReader(fileName))
            {
                commandItems = xs.Deserialize(xr) as CommandItems;
                xr.Close();
            }
            commandItems.ConfigFile = fileName;
            return commandItems;
        }
    }


    public class CommandItem : INotifyPropertyChanged
    {
        private bool _active;
        private string _commandName;
        private CommandTypes _commandType;

        public CommandItem()
        {
            CommandType = CommandTypes.VisualStudioCommand;
        }

        public CommandTypes CommandType
        {
            get { return _commandType; }
            set
            {
                _commandType = value;
                switch (_commandType)
                {
                    case CommandTypes.VisualStudioCommand:
                        Command = "cmd";
                        string bat = "\"" + @"C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" + "\"";
                        Arguments = @" /k " + bat + " x86";
                        break;
                    case CommandTypes.DelphiCommand:
                        Command = "cmd";
                        string bat1 = "\"" + @"C:\Program Files\CodeGear\RAD Studio\5.0\bin\rsvars.bat" + "\"";
                        Arguments = @" /k " + bat1;
                        break;
                    case CommandTypes.GeneralCommand:
                        break;
                }
            }
        }

        public string CommandName
        {
            get { return _commandName; }
            set
            {
                _commandName = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("CommandName"));
            }
        }

        public string Command { get; set; }
        public string Arguments { get; set; }

        public string BuildCommand { get; set; }

        public bool Active
        {
            get { return _active; }
            set
            {
                _active = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Active"));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public override string ToString()
        {
            return CommandName;
        }
    }
}